/*
 * Diagram.h
 *
 *  Created on: 11.04.2010
 *      Author: king
 */

#ifndef DIAGRAM_H_
#define DIAGRAM_H_

#include <QWidget>
#include <QPair>
#include <QGraphicsView>

#include <vector>
#include "Node.h"
#include "genealogy_tree/PersonType.h"
#include "genealogy_tree/Person.h"
#include "genealogy_tree/GenealogyTree.h"
#include "db/DataSourceManager.h"

class QAction;
class QGraphicsItem;
class QGraphicsScene;

namespace etu {
namespace ui {

class CLink;

using etu::genealogy_tree::GenealogyTree;
using etu::genealogy_tree::PersonType;

class CDiagram: public QGraphicsView
{
    Q_OBJECT

public:
    CDiagram(QWidget *parent = 0);
    ~CDiagram();
    GenealogyTree* getTree();
    void setTree(GenealogyTree* tree);
    CNode *selectedNode() const;
    void init(etu::db::DataSource* dataSource);
    void clear();


signals:
  void deletePerson(bool);
  void addFather(PersonType);
  void addMother(PersonType);
  void addChild(PersonType);
  void selectPerson(Person*, QPixmap*);

private slots:
    void addFather();
    void addMother();
    void addChild();
    void deletePerson();
    void bringToFront();
    void sendToBack();
    void updateActions();
    void updateCurrentNode();
    void createNewPerson(Person, PersonType);
    void createLink(CNode* first, CNode* second);
    void createChildNode(CNode* parentNode, CNode* childNode);
    void updatePhoto(QPixmap*);
    void search(Person*);
    void showSibs();

private:
    typedef QPair<CNode *, CNode *> NodePair;

    void createActions();
    void setZValue(int z);
    void setupNode(CNode *node, CNode *parent);
    void loadPhoto();
    void replotTree();

    CLink *selectedLink() const;
    NodePair selectedNodePair() const;
    void removeDeletedPersons();
    QList<CNode* > getNodes(QList<QGraphicsItem* > items);

    QAction *addMotherAction;
    QAction *addFatherAction;
    QAction *addChildAction;
    QAction *deleteAction;

    QGraphicsScene* m_scene;
    GenealogyTree* m_tree;
    etu::db::DataSource* m_dataSource;

    int minZ;
    int maxZ;
    int seqNumber;

    std::vector<QAction*> m_actions;
    std::vector<CNode*> m_foundedNodes;
};

}
}
#endif /* DIAGRAM_H_ */
